DECLARE @FILIAL CHAR(25)
SELECT @FILIAL = 'SP-BOURBON'

UPDATE 
	A 
SET 
	ESTOQUE = B.TOTAL_MOVIMENTO,
	ES1 = B.EN1, ES2 = B.EN2, ES3 = B.EN3, ES4 = B.EN4, ES5 = B.EN5, ES6 = B.EN6, ES7 = B.EN7, ES8 = B.EN8,
	ES9 = B.EN9, ES10 = B.EN10, ES11 = B.EN11, ES12 = B.EN12, ES13 = B.EN13, ES14 = B.EN14, ES15 = B.EN15, ES16 = B.EN16, 
	ES17 = B.EN17, ES18 = B.EN18, ES19 = B.EN19, ES20 = B.EN20, ES21 = B.EN21, ES22 = B.EN22, ES23 = B.EN23, ES24 = B.EN24, 
	ES25 = B.EN25, ES26 = B.EN26, ES27 = B.EN27, ES28 = B.EN28, ES29 = B.EN29, ES30 = B.EN30, ES31 = B.EN31, ES32 = B.EN32, 
	ES33 = B.EN33, ES34 = B.EN34, ES35 = B.EN35, ES36 = B.EN36, ES37 = B.EN37, ES38 = B.EN38, ES39 = B.EN39, ES40 = B.EN40, 
	ES41 = B.EN41, ES42 = B.EN42, ES43 = B.EN43, ES44 = B.EN44, ES45 = B.EN45, ES46 = B.EN46, ES47 = B.EN47, ES48 = B.EN48
FROM 
	ESTOQUE_PRODUTOS A
	LEFT JOIN 
	(SELECT 
		FILIAL, PRODUTO, COR_PRODUTO, 
		SUM(TOTAL_MOVIMENTO) AS TOTAL_MOVIMENTO,
		SUM(EN1) AS EN1, SUM(EN2) AS EN2, SUM(EN3) AS EN3, SUM(EN4) AS EN4, SUM(EN5) AS EN5, SUM(EN6) AS EN6, 
		SUM(EN7) AS EN7, SUM(EN8) AS EN8, SUM(EN9) AS EN9, SUM(EN10) AS EN10, SUM(EN11) AS EN11, SUM(EN12) AS EN12, 
		SUM(EN13) AS EN13, SUM(EN14) AS EN14, SUM(EN15) AS EN15, SUM(EN16) AS EN16, SUM(EN17) AS EN17, SUM(EN18) AS EN18, 
		SUM(EN19) AS EN19, SUM(EN20) AS EN20, SUM(EN21) AS EN21, SUM(EN22) AS EN22, SUM(EN23) AS EN23, SUM(EN24) AS EN24, 
		SUM(EN25) AS EN25, SUM(EN26) AS EN26, SUM(EN27) AS EN27, SUM(EN28) AS EN28, SUM(EN29) AS EN29, SUM(EN30) AS EN30, 
		SUM(EN31) AS EN31, SUM(EN32) AS EN32, SUM(EN33) AS EN33, SUM(EN34) AS EN34, SUM(EN35) AS EN35, SUM(EN36) AS EN36, 
		SUM(EN37) AS EN37, SUM(EN38) AS EN38, SUM(EN39) AS EN39, SUM(EN40) AS EN40, SUM(EN41) AS EN41, SUM(EN42) AS EN42, 
		SUM(EN43) AS EN43, SUM(EN44) AS EN44, SUM(EN45) AS EN45, SUM(EN46) AS EN46, SUM(EN47) AS EN47, SUM(EN48) AS EN48 
	FROM 
		LX_RECALCULA_ESTOQUE_FRANQUIA (NULL, NULL, @FILIAL, NULL, NULL)
	GROUP BY
		FILIAL, PRODUTO, COR_PRODUTO
	)
	B ON A.FILIAL = B.FILIAL AND A.PRODUTO = B.PRODUTO AND A.COR_PRODUTO = B.COR_PRODUTO
WHERE 
	A.FILIAL = @FILIAL
